import 'package:flutter/material.dart';
import '../../entity/user.dart';

class UserAvatar extends StatelessWidget {
  final User? user;
  final double radius;
  final VoidCallback? onTap;

  const UserAvatar({
    super.key,
    required this.user,
    this.radius = 20.0,
    this.onTap,
  });

  @override
  Widget build(BuildContext context) {
    final bool hasAvatar = user != null && user!.avatar.isNotEmpty;

    ImageProvider? backgroundImage;
    if (hasAvatar) {
      backgroundImage = AssetImage(user!.avatar);
    }

    return GestureDetector(
      onTap: onTap,
      child: CircleAvatar(
        radius: radius,
        backgroundColor: Colors.blueGrey,
        backgroundImage: backgroundImage,
        child: !hasAvatar
            ? Text(
          user?.name.substring(0, 1).toUpperCase() ?? 'U',
          style: TextStyle(
            color: Colors.white,
            fontWeight: FontWeight.bold,
            fontSize: radius,
          ),
        )
            : null,
      ),
    );
  }
}
